package org.ly.service;

import com.google.gson.Gson;
import org.ly.dao.*;
import org.ly.util.LogUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;

@Component
public class MetricChecker {
    private final HostMetricRepository hostMetricRepository;
    private final TempMessageRepository tempMessageRepository;
    private final CmdbHostRepository cmdbHostRepository;

    public MetricChecker(HostMetricRepository hostMetricRepository, TempMessageRepository tempMessageRepository, CmdbHostRepository cmdbHostRepository) {
        this.hostMetricRepository = hostMetricRepository;
        this.tempMessageRepository = tempMessageRepository;
        this.cmdbHostRepository = cmdbHostRepository;
    }

    public void nodataCheck(String message) {
        List<TempMessage> tempMessages = new TempMessageService(tempMessageRepository).eopsMetricMessageResolve(message);
        for (TempMessage tempMessage : tempMessages) {
            if (tempMessage.getRawMessage().contains("nodata")) {
                LogUtil.logInfo("nodata消息单元：" + tempMessage.getRawMessage());
                HostMetric hostMetric = new HostMetric();
                hostMetric.setReported(false);
                hostMetric.setHostIp(tempMessage.getHostIp());
                hostMetric.setCollectTime(tempMessage.getCollectTime());
                hostMetric.setCompKey(tempMessage.getCompKey());
                hostMetric.setCompType(tempMessage.getCompType());
                hostMetric.setExceptionType("nodata");
                hostMetricRepository.save(hostMetric);
            }
        }
    }

    public void nothingCheck(String message) {
        List<TempMessage> tempMessages = new TempMessageService(tempMessageRepository).eopsConfigMessageResolve(message);
        this.saveTempMessages(tempMessages);
    }

    public void saveTempMessages(List<TempMessage> tempMessages) {
        List<String> messageIps = tempMessageRepository.findAllMessageIp();
        tempMessages.removeIf(tempMessage -> messageIps.contains(tempMessage.getHostIp()));
        tempMessageRepository.saveAll(tempMessages);
    }

    @Scheduled(cron = "${custom-config.scheduled.metricLostCheckCron}")
    public void iPLostCheck() {
        Timestamp fiveMinutesAgo = Timestamp.valueOf(LocalDateTime.now().minusMinutes(5));
        List<TempMessage> tempMessages = tempMessageRepository.findAll();
        HashSet<String> messageIpSet = new HashSet<>();
        for(TempMessage tempMessage: tempMessages) {
            String hostIP = tempMessage.getHostIp();
            messageIpSet.add(hostIP);
        }
        String onlineStatus = "[{\"code\":\"12\",\"name\":\"在用\",\"type\":\"enum\"}]";
        List<CmdbHost> cmdbHostList = cmdbHostRepository.findCmdbHostByHostState(onlineStatus);
        HashSet<String> cIpSet = new HashSet<>();
        for(CmdbHost cmdbHost: cmdbHostList) {  // todo 检查
            String cHostIp = cmdbHost.getHostInnerip().replace("\"", "");
            cIpSet.add(cHostIp);
        }
        Set<String> difference = new HashSet<>(cIpSet);
//        System.out.println("cmdb的IP：" + difference);
//        System.out.println("message的IP：" + messageIpSet);
        difference.removeAll(messageIpSet);
//        System.out.println("差集IP：" + difference);
        for (String s: difference){
            HostMetric hostMetric = new HostMetric();
            hostMetric.setReported(Boolean.FALSE);
            hostMetric.setHostIp(s);
            hostMetric.setExceptionType("ipLost");
            hostMetric.setCollectTime(new Timestamp(System.currentTimeMillis()));
            hostMetricRepository.save(hostMetric);
        }

        tempMessageRepository.deleteAll(tempMessages);
    }

}
